#include "ruler.h"
#include <QPainter>

const static int s_fontSizeLarge = 14;
const static int s_fontSize = 8;

Ruler::Ruler(QWidget *parent)
    : QWidget{parent}
{
}
qreal Ruler::unit() const
{
    return m_unit;
}

void Ruler::setUnit(qreal unit)
{
    if (qFuzzyCompare(m_unit, unit))
        return;
    m_unit = unit;
}



int Ruler::getLength() const
{
    return m_orientation == Qt::Horizontal ? this->width() : this->height();
}

QRect Ruler::calcRect(int offset)
{
    if(m_orientation == Qt::Horizontal){
        return QRect(offset, 0, m_unitSpace, height());
    }else{
        return QRect(0, this->height() - offset - m_unitSpace, width(), m_unitSpace);
    }
}


void Ruler::changedStart(QPointF position)
{
    if(m_orientation == Qt::Horizontal){
        setStart(position.x());
    }else{
        setStart(position.y());
    }

}

void Ruler::changedZoom(int space, qreal unit)
{
    if (m_unitSpace== space && qFuzzyCompare(m_unit, unit))
        return;
    m_unitSpace = space;
    m_unit = unit;
    repaint();
}

Qt::Orientation Ruler::orientation() const
{
    return m_orientation;
}

void Ruler::setOrientation(Qt::Orientation newOrientation)
{
    if (m_orientation == newOrientation)
        return;
    m_orientation = newOrientation;
    repaint();
}

void Ruler::setAlign(Qt::Alignment align)
{
    m_align = align;
}

Qt::Alignment Ruler::align() const
{
    return m_align;
}

int Ruler::space() const
{
    return m_space;
}

void Ruler::setSpace(int newSpace)
{
    m_space = newSpace;
}

qreal Ruler::getStart() const
{
    return m_start;
}

void Ruler::setStart(qreal newStart)
{
    if (qFuzzyCompare(m_start, newStart))
        return;
    m_start = newStart;
    repaint();
}

int Ruler::unitSpace() const
{
    return m_unitSpace;
}

void Ruler::setUnitSpace(int space)
{
    m_unitSpace = space;
}


void Ruler::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);
    qreal scale = m_unitSpace / m_unit;
    //预留显示数字的空间
    int length = getLength() - m_space * 2;
    if(length < 0)
        return;

    int offset = m_space - m_unitSpace / 2;
    qreal num = qCeil(m_start / m_unit) * m_unit; //标尺开始的数字
    int pos = (num - m_start) * scale + offset;
    int count = length / m_unitSpace;



    QPainter painter(this);

    //每个单位距离
    for(int i = 0; i <= count; ++ i){

        QRect textRect = calcRect(pos);

        if(m_orientation == Qt::Horizontal){
            if(textRect.x() > length + offset)
                continue;
        }else{
            if(textRect.y() < m_space)
                continue;
        }

        QFont font = painter.font();
        if(qRound(num / m_unit) % 10 == 0){
            font.setPixelSize(s_fontSizeLarge);
            painter.setFont(font);

        }else{
            font.setPixelSize(s_fontSize);
            painter.setFont(font);
        }

        painter.drawText(textRect, m_align, QString::number(num));

        num = QString::asprintf("%.3f", num + m_unit).toDouble();
        pos += m_unitSpace;
    }
    
}
